package com.example.demo.materialPurchase.mapper;

import com.example.demo.entity.Department;
import com.example.demo.entryWarehouse.entity.BaseInformation;
import com.example.demo.materialPurchase.entity.MaterialBaseInfo;
import com.example.demo.materialPurchase.entity.MaterialPurchase;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lby
 * @since 2021-05-09
 */
public interface MaterialPurchaseMapper extends BaseMapper<MaterialPurchase> {

	@Insert("insert into material_purchase (table_id,material_name,material_code,depart_name,purchase_num,"
			+ "part_used,purchase_status,remark) values (#{tableId},#{materialName},#{materialCode},"
			+ "#{departName},#{purchaseNum},#{partUsed},#{purchaseStatus},#{remark})")
	int insertOneNew(@Param("tableId") String tableId,@Param("materialName") String materialName,@Param("materialCode") String materialCode,
			@Param("departName") String departName,@Param("purchaseNum") int purchaseNum,@Param("partUsed") String partUsed,
			@Param("purchaseStatus") String purchaseStatus,@Param("remark") String remark);
	
	@Select("select id from department_manage where depart_name = #{departName}")
	public Department seldepId(@Param("departName") String departName);
	
	@Update("update material_purchase set purchase_status = #{purchaseStatus} where id = #{id}")
	int UpdateStatus(@Param("purchaseStatus") String purchaseStatus, @Param("id") int id);
	
	@Select("SELECT material_purchase.id,table_id,material_name,material_code,depart_name,purchase_num,part_used,purchase_status,\r\n" + 
			"remark,specification,unit \r\n" + 
			"FROM material_purchase "+
			"LEFT JOIN base_information ON material_name = base_information.`name` "+ 
			" ${ew.customSqlSegment} " )
	public IPage<MaterialBaseInfo> selAllPurchase(Page<MaterialBaseInfo> page, @Param(Constants.WRAPPER) Wrapper<MaterialBaseInfo> wrapper );
	
	@Select("SELECT material_purchase.id,table_id,material_name,material_code,depart_name,purchase_num,part_used,purchase_status,\r\n" + 
			"remark,specification,unit \r\n" + 
			"FROM base_information,material_purchase \r\n" + 
			"where material_purchase.material_name = base_information.`name` \r\n" + 
			"and material_purchase.purchase_status = #{preStatus} "
			+ " and table_id like #{currentMonth}; ")
	public List<MaterialBaseInfo> selAllPurByStatus(@Param("preStatus") String preStatus, @Param("currentMonth") String currentMonth);
	
	@Select("select d.*\r\n" + 
			"from department_manage d, repo_role r, repo_user u, repo_user_role ur \r\n" + 
			"where u.account = #{account} and u.id = ur.u_id and ur.r_id = r.id and r.d_id = d.id")
	public Department selAccountDepart(@Param("account") String account);
}
